import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreatePermissionControlDto } from './dto/create-permission-control.dto';
import { UpdatePermissionControlDto } from './dto/update-permission-control.dto';
import { PermissionControlEntity } from './permission-control.entity';

@Injectable()
export class PermissionControlService {

  constructor(
    @InjectRepository(PermissionControlEntity)
    private readonly permissionControlRepo: Repository<PermissionControlEntity>,
  ) {
  }

  create(createPermissionControlDto: CreatePermissionControlDto) {
    const p = this.permissionControlRepo.create(createPermissionControlDto);
    return this.permissionControlRepo.save(p);
  }

  async update(id: number, updatePermissionControlDto: UpdatePermissionControlDto) {
    const p = await this.permissionControlRepo.findOneById(id);
    if (p) {
      return this.permissionControlRepo.updateById(id, updatePermissionControlDto);
    }
    throw new NotFoundException('未找到该条数据');
  }
}
